//
//  LLNetWork.m
//  LLFoundation
//
//  Created by wangzhaomeng on 16/8/16.
//  Copyright © 2016年 MaoChao Network Co. Ltd. All rights reserved.
//

#import "LLNetWork.h"
#import "AFNetworking.h"
//#import "NSString+AddPart.h"

@implementation LLNetWork

+ (AFHTTPSessionManager *)getManager{
    static dispatch_once_t onceToken;
    static AFHTTPSessionManager *manager;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        //manager.requestSerializer = [AFJSONRequestSerializer serializer];//设置请求数据为json
        manager.responseSerializer = [AFJSONResponseSerializer serializer];//设置返回数据为json
        manager.requestSerializer.stringEncoding = NSUTF8StringEncoding;
        manager.requestSerializer.timeoutInterval = 10;
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
                                                                                  @"text/html",
                                                                                  @"text/json",
                                                                                  @"text/plain",
                                                                                  @"text/javascript",
                                                                                  @"text/xml",
                                                                                  @"image/*"]];
    });
    return manager;
}

+ (NSURLSessionDataTask *)getDataWithURLString:(NSString *)URLString callBack:(void(^)(id responseObject,NSError *error))callBack{
    AFHTTPSessionManager *manager = [self getManager];
    NSURLSessionDataTask *dataTask = [manager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (callBack) {
            callBack(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task,id  _Nullable responseObject, NSError * _Nonnull error) {
        if (callBack) {
            callBack(nil,error);
        }
    }];
    return dataTask;
}

+ (NSURLSessionDataTask *)postDataWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters callBack:(void(^)(id responseObject,NSError *error))callBack{
    AFHTTPSessionManager *manager = [self getManager];
    NSURLSessionDataTask *dataTask = [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (callBack) {
            callBack(responseObject,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task,id  _Nullable responseObject, NSError * _Nonnull error) {
        if (callBack) {
            callBack(nil,error);
        }
    }];
    return dataTask;
}



@end
